package com.situ.object.controll;


import com.fasterxml.jackson.databind.ObjectMapper;
import com.situ.object.common.Page;
import com.situ.object.common.ResponseBean;
import com.situ.object.entity.Student;
import com.situ.object.entity.Teacher;
import com.situ.object.service.StudentService;
import com.situ.object.service.impl.StudentServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/student")
public class StudentServlet extends HttpServlet {
    //合成
    private StudentService studentService = new StudentServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置req编码格式和resp内容类型
        req.setCharacterEncoding("utf-8");
        resp.setContentType("application/json;charset=utf-8");

        //二、获取参数
        //根据jsp页面的ajax传输地址中的method的属性值进行分支
        String method = req.getParameter("method");
        if ("getForPage".equals(method)){
            getForPage(req,resp);
        }else if ("add".equals(method)){
            add(req,resp);
        }else if ("edit".equals(method)){
            edit(req,resp);
        }else if ("delete".equals(method)){
            delete(req,resp);
        }else if("selectAll".equals(method)){
            selectAll(req,resp);
        }
        else {
            System.out.println("----------");
        }

    }

    private void selectAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<Student> list = studentService.selectAll();
        System.out.println("list"+list);

        //创建 ObjectMapper 对象
        ObjectMapper objectMapper = new ObjectMapper();
        //将 Java 对象序列化为JSON 字符串
        String jsonStr = objectMapper.writeValueAsString(ResponseBean.ok(0,list));
        resp.getWriter().append(jsonStr);
    }

    private void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //删除，根据id来删除用户
        //获取参数
        Integer goods_id= Integer.valueOf(req.getParameter("id"));

        try{

            studentService.delete(goods_id);

            //返回s
            //创建jackson的核心对象
            ObjectMapper objectMapper=new ObjectMapper();
            String jsonStr=objectMapper.writeValueAsString(ResponseBean.ok());
            resp.getWriter().append(jsonStr);
        }catch (Exception e){
            ObjectMapper objectMapper=new ObjectMapper();
            String jsonStr=objectMapper.writeValueAsString(ResponseBean.failed(e.getMessage()==null?"删除失败":e.getMessage()));
            resp.getWriter().append(jsonStr);
        }
    }

    private void edit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取参数  Integer.valueOf 转为int类型
        Integer id = Integer.valueOf(req.getParameter("id"));
        String name = req.getParameter("student_name");
        String password = req.getParameter("student_password");
        String sex = req.getParameter("student_sex");
        Integer age = Integer.valueOf(req.getParameter("student_age"));
        String teacher_id = req.getParameter("teacher_id");
        String status = req.getParameter("status");
        String email = req.getParameter("email");
        String photo = req.getParameter("photo");



        //调用service 首先合成
        Student student = new Student();
        student.setStudentId(id);
        student.setStudentName(name);
        student.setStudentPassword(password);
        student.setStudentSex(sex);
        student.setEmail(email);
        student.setTeacherId(Integer.valueOf(teacher_id));
        student.setPhoto(photo);
        student.setStatus(Integer.valueOf(status));
        student.setStudentAge(age);


        try {

            studentService.edit(student);

            ObjectMapper objectMapper = new ObjectMapper();
            String jsonStr = objectMapper.writeValueAsString(ResponseBean.ok());
            resp.getWriter().append(jsonStr);
        } catch (Exception e) {
            ObjectMapper objectMapper = new ObjectMapper();
            String jsonStr = objectMapper.writeValueAsString(ResponseBean.failed(e.getMessage()==null?"编辑失败":e.getMessage()));
            resp.getWriter().append(jsonStr);
        }
    }

    private void add(HttpServletRequest req, HttpServletResponse resp)  throws ServletException, IOException {
        //获取参数
        String name = req.getParameter("student_name");
        String password = req.getParameter("student_password");
        String sex = req.getParameter("student_sex");
        String email = req.getParameter("email");
        String teacher_id = req.getParameter("teacher_id");
        String photo = req.getParameter("photo");
//        String status = req.getParameter("status");
        Integer age = Integer.valueOf(req.getParameter("student_age"));

        //将参数set给用户的实现类对象
        Student student = new Student();
        student.setStudentName(name);
        student.setStudentPassword(password);
        student.setStudentSex(sex);
        student.setEmail(email);
        student.setTeacherId(Integer.valueOf(teacher_id));
        student.setPhoto(photo);
//        student.setStatus(Integer.valueOf(status));
        student.setStudentAge(age);

        //调用service 首先合成类
        int i = studentService.add(student);
        System.out.println(student);
        if (i > 0){
            ObjectMapper objectMapper = new ObjectMapper();
            String jsonStr = objectMapper.writeValueAsString(ResponseBean.ok());
            resp.getWriter().append(jsonStr);
        }else {
            ObjectMapper objectMapper = new ObjectMapper();
            String jsonStr = objectMapper.writeValueAsString(ResponseBean.failed("添加失败"));
            resp.getWriter().append(jsonStr);
        }
    }


    private void getForPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取参数 page 当前页面 limit 每页条数
        Integer page = Integer.valueOf(req.getParameter("page"));
        Integer limit = Integer.valueOf(req.getParameter("limit"));

        //多条件查询 学生姓名和教师id
        Student student = new Student();
        student.setStudentName(req.getParameter("student_name"));
        //处理teacher_id为空的情况
        try{
            student.setTeacherId(Integer.valueOf(req.getParameter("teacher_id")));
        }catch (Exception e){
            System.out.println("teacher_id为空");
        }
//        System.out.println(student);
        //调用业务处理
        System.out.println("studentServlet："+student);
        Page pageInfo = studentService.getForPage(page,limit,student);
        System.out.println("student_pageInfo:"+pageInfo);
        //返回
        //创建jackson的核心对象
        ObjectMapper objectMapper=new ObjectMapper();
        String jsonStr=objectMapper.writeValueAsString(ResponseBean.ok(0,pageInfo));
        resp.getWriter().append(jsonStr);
    }



    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}


